<template>
    <div>
        <div style="padding: 0px 0 ;">
            <el-input v-model="input" style="width:200px;" placeholder="根据菜单名称查找"
                suffix-icon="el-icon-search"></el-input>
            <el-button style="margin-left: 10px;" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>
        <div style="margin:10px 0;">
            <el-button type="primary" @click="handAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
        </div>

        <el-table :data="tableData" style="max-height: 500px;overflow: auto;">
            <el-table-column prop="name" label="角色名" width="120">
            </el-table-column>
            <el-table-column prop="flag" label="描述" width="140">
            </el-table-column>
            <el-table-column prop="description" label="描述" width="140">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="info" @click="selectMenu(scope.row)">分配菜单 <i class="el-icon-menu"></i></el-button>
                    <el-button type="success" @click="handEdit(scope.row)">编辑</el-button>
                    <el-popconfirm style="margin-left: 5px;" confirm-button-text='确定' cancel-button-text='我在想想'
                        icon="el-icon-info" icon-color="red" title="您确定删除吗？" @confirm="deleteById(scope.row.id)">
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="padding: 10px 0;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
                :page-sizes="[3, 6, 9, 12]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog title="角色信息" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="名称" label-width="120px">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="唯一标识" label-width="120px">
                    <el-input v-model="form.flag" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" label-width="120px">
                    <el-input v-model="form.description" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelDialog">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="菜单分配" :visible.sync="menuDialogFormVisible">
            <el-tree :data="menuData" :props="props" show-checkbox node-key="id" :default-expanded-keys="expends"
                :default-checked-keys="checks" ref="tree" 在树节点上  :check-strictly="true">
                <span class="custom-tree-node" slot-scope="{ node, data }">
                    <span><i :class="data.icon"></i> {{ data.name + data.id }}</span>
                </span>
            </el-tree>
            <div slot="footer" class="dialog-footer">
                <el-button @click="canceMenulDialog">取 消</el-button>
                <el-button type="primary" @click="saveRoleMenu">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "Role",
    data() {
        return {
            tableData: null,
            currentPage4: 4,
            total: 0,
            pageNum: 1,
            pageSize: 3,
            input: '',
            dialogFormVisible: false,
            menuDialogFormVisible: false,
            form: '',
            menuData: [],
            props: {
                label: 'name'
            },
            expends: [],
            checks: [],//弹窗树形控件展开
            roleId: 0,
            ids: [],
            roleFlag: '',
        }
    },
    created() {
        this.load();
    },
    methods: {
        cancelDialog() {
            this.dialogFormVisible = false;
        },
        canceMenulDialog() {//菜单弹窗取消
            this.menuDialogFormVisible = false;
        },
        async selectMenu(role) {
            this.roleFlag = role.flag
            this.roleId = role.id
            this.request.get("/menu").then(res => {
                this.menuData = res.data;
                //把后台返回的菜单数据处理成id数组
                this.expends = this.menuData.map(v => v.id);
            })
            this.request.get("/role/roleMenu/" + this.roleId).then(res => {
                this.checks = res.data;
                this.ids.forEach(id => {
                    if (!this.checks.includes(id)) {
                        console.log("此时id=》"+id)
                        // 可能会报错：Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'setChecked')
                        this.$nextTick(() => {
                            this.$refs.tree.setChecked(id, false)
                        })
                    }
                })
                this.menuDialogFormVisible = true;
            })
        },
        handleCheckChange(data, checked, indeterminate) {
            //console.log(data, checked, indeterminate);
        },
        deleteById(id) {
            this.request.delete(`/role/${id}`).then(res => {
                if (res.code == 200) {
                    this.$message.success('删除成功')
                    this.load()
                } else {
                    this.$message.success('删除失败')
                }
            })
        },
        saveRoleMenu() {//分配菜单提交
            console.log(this.$refs.tree.getCheckedNodes());
            this.request.post("/role/roleMenu/" + this.roleId, this.$refs.tree.getCheckedKeys()).then(res => {
                console.log("当前getCheckedKeys==>",this.$refs.tree.getCheckedKeys())
                if (res.code == 200) {
                    this.$message.success("绑定成功")
                    this.menuDialogFormVisible = false
                    // 操作管理员角色后需要重新登录
                    if (this.roleFlag === 'ROLE_ADMIN') {
                        this.$store.commit("logout")
                    }
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        save() {//新增提交数据方法
            console.log(this.form);
            this.request.post("/role", this.form).then(res => {
                if (res.code == 200) {
                    this.$message.success("保存成功")
                    this.dialogFormVisible = false
                    this.load();
                } else {
                    this.$message.error("保存失败")
                }
            })
            this.dialogFormVisible = false
            this.load();
        },
        handEdit(row) {//打开编辑窗口
            this.form = row
            this.dialogFormVisible = true;
        },
        handAdd() {//打开新增窗口
            this.dialogFormVisible = true;
            this.form = {};
        },
        reset() {//重置按钮方法
            this.input = "";
            this.load();
        },
        load() {//表格刷新
            this.request.get("/role/page", {
                params: {
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
                    name: this.input
                }
            }).then(res => {
                this.tableData = res.data.records;
                this.total = res.data.total;
            })

            this.request.get("/menu/ids").then(r => {

                this.ids = r.data
                console.log("此时ids==》" + this.ids);
            })

        },
        handleSizeChange(pageSize) {//分页每页条数
            this.pageSize = pageSize;
            this.load();
        },
        handleCurrentChange(pageNum) {//分页页码选择
            this.pageNum = pageNum;
            this.load();
        }
    }
}
</script>

<style scoped>
.el-table::before {
    height: 0px;
}
</style>